package com.abel.bigwater.wflow.mapper

import com.abel.bigmeter.service.UserOperParam
import com.abel.bigwater.model.BwConfig
import com.abel.bigwater.model.BwFirm
import com.abel.bigwater.model.BwUserOper
import org.apache.ibatis.annotations.*
import java.util.*

@Mapper
interface ConfigMapper {

    @Select("""<script>
        SELECT group_id AS groupId,
        config_id AS configId,
        config_value AS value,
        config_name AS configName,
        config_type AS configType,
        config_desc AS configDesc,
        c.firm_id AS firmId,
        f.firm_name AS firmName
        FROM bw_config c LEFT JOIN bw_firm f ON c.firm_id = f.firm_id
        <where>
            <if test="configId != null">
               config_id = #{configId}
            </if>
            <if test="firmId != null">
               AND c.firm_id LIKE #{firmId}
            </if>
            <if test="groupId != null">
                AND group_id = #{groupId}
            </if>
        </where>
        </script>""")
    fun configList(@Param("firmId") firmId: String?, @Param("groupId") groupId: String?, @Param("configId") configId: String?): List<BwConfig>

    @Update("""<script>
        UPDATE bw_config
        SET config_value = #{value}
        <where>
            <if test="configId != null">
               config_id = #{configId}
            </if>
            <if test="firmId != null">
               AND firm_id LIKE #{firmId}
            </if>
            <if test="groupId != null">
                AND group_id = #{groupId}
            </if>
        </where>
        </script>""")
    fun updateConfig(config: BwConfig): Int

    @Insert("""
        INSERT INTO bw_config(config_id, group_id, config_name,
        config_type, config_desc, config_value, firm_id)
        VALUES(#{configId}, #{groupId}, #{configName},
        #{configType}, #{configDesc}, #{value}, #{firmId})
        """)
    fun insertConfig(config: BwConfig): Int

    @Insert("""
        INSERT INTO bw_user_oper(user_id, oper_time, return_time, firm_id, oper_case,
        oper_role, oper_right, oper_result, oper_desc)
        VALUES(#{userId}, #{operTime}, #{returnTime}, #{firmId}, #{operCase},
        #{operRole}, #{operRight}, #{operResult}, #{operDesc})
        """)
    @Options(useGeneratedKeys = true, keyProperty = "operId", keyColumn = "oper_id")
    fun insertUserOper(userOper: BwUserOper): Int

    @Select("""<script>
        SELECT oper_id AS operId
        , user_id AS userId
        , oper_time AS operTime
        , return_time AS returnTime
        , firm_id AS firmId
        , oper_case AS operCase
        , oper_role AS operRole
        , oper_right AS operRight
        , oper_result AS operResult
        , oper_desc AS operDesc
        FROM bw_user_oper
        <where>
            <when test='firmId != null'>
                firm_id LIKE #{firmId}
            </when>
            <when test='operTime1 != null'>
                AND oper_time &gt;= #{operTime1}
            </when>
            <when test='operTime2 != null'>
                AND oper_time &lt; #{operTime2}
            </when>
            <when test='userId != null'>
                AND user_id = #{userId}
            </when>
            <when test='operCase != null'>
                AND oper_case = #{operCase}
            </when>
        </where>
        </script>""")
    fun listUserOper(param: UserOperParam): List<BwUserOper>

    @Select("""<script>
        SELECT user_id AS userId
        , oper_case AS operCase
        , COUNT(1) AS operCount
        FROM bw_user_oper
        <where>
            <when test='firmId != null'>
                firm_id LIKE #{firmId}
            </when>
            <when test='operTime1 != null'>
                AND oper_time &gt;= #{operTime1}
            </when>
            <when test='operTime2 != null'>
                AND oper_time &lt; #{operTime2}
            </when>
            <when test='userId != null'>
                AND user_id = #{userId}
            </when>
            <when test='operCase != null'>
                AND oper_case = #{operCase}
            </when>
        </where>
        GROUP BY user_id, oper_case
        </script>""")
    fun statUserOper(param: UserOperParam): List<BwUserOper>

    /**
     * <pre>
     * <parameterMap type="map" id="firmMap">
     * <parameter property="firmId"></parameter>
    </parameterMap> *
    </pre> *
     *
     * @param map
     * @return
     */
    @Select("""<script>
        SELECT firm_id as `id`,
        firm_name as `name`,
        ST_AsText(firm_loc) AS firmLoc,
        ST_AsText(firm_region) AS firmRegion
        FROM bw.bw_firm
        <where>
            <if test='firmId != null'>
                firm_id LIKE #{firmId}
            </if>
            <if test='keywords != null'>
                AND MATCH(firm_id, firm_name) AGAINST (#{keywords} IN BOOLEAN MODE)
            </if>
        </where>
        </script>""")
    fun selectFirm(@Param("firmId") firmId: String?, @Param("keywords") keywords: String? = null): List<BwFirm>

    @Insert("""
        INSERT INTO bw_firm(firm_id, firm_name,
        firm_loc, firm_region)
        VALUES(#{id}, #{name},
        ST_GeomFromText(#{firmLoc}),
        ST_GeomFromText(#{firmRegion}))
        """)
    fun addFirm(firm: BwFirm): Int

    @Delete("""DELETE FROM bw_firm
        WHERE firm_id = #{firmId}
        """)
    fun deleteFirm(@Param("firmId") firmId: String): Int

    @Update("""<script>
        UPDATE bw_firm
        <set>
            <when test='name != null'>
                firm_name = #{name},
            </when>
            <when test='firmLoc != null'>
                firm_loc = ST_GeomFromText(#{firmLoc}),
            </when>
            <when test='firmRegion != null'>
                firm_region = ST_GeomFromText(#{firmRegion})
            </when>
        </set>
        WHERE firm_id = #{id}
        </script>""")
    fun updateFirm(firm: BwFirm): Int
}